package com.company.example.core.config;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.company.example.core.util.encrypt.BASE64Util;
import com.company.example.core.util.encrypt.RSAUtil;


public class PropertyPlaceholderConfigurerExt extends PropertyPlaceholderConfigurer{
	
	private final static String PRIVATEKEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJBoryeMW48miDtqO6bKVN4VZ5WaNtO8VLDIVX5NchAvlS8mv5wHOdkeZBNDD2wcHu5e4QjdEelNJbFsoG3pWs1/g5KWMIzae0Gbhjzj5sv0102U0x3DXt75stVkEycmJ3Po9fCk9ImGF5K7d6VDazCYpeDTPcIkcd8TFMNv/MndAgMBAAECgYAiDo5AZXmyaQP3/zXSQCWDusiTH6uOg/v1NUn0X49nAdHNLPZDdfDOo7w2Trp0ZK1myjmIPZrbyrX5AgVT5pTjRqfw70KMawo6U3gUFnMLngiEe4ULAhGlCO1cg1MPzH968x3Sdzk3YhXnxIN4Hwbf2TmAOXiU9XGAh5aNh1YGgQJBANnxPnmKuzYDZRI/EF1bW0MdQsgf5YzaTR8cX2bZVv2DqXTU5LZl+Gg5YyHN1v0oGdhJicH7foCYnp18oGh3IwMCQQCpoD+WOtOJl5LmyW8RSr5TDK8KBlMgT/rjSxub+V0my/L1CzkqQXjQQ8lmEK997k3fNkmArq3xH+ke0Y8wRlmfAkEAjm8SxH49ccRwNqeOe2TsVQV3krD95br58h4p316lcpOsmNVWEDq+SOdQeVQ8cEDX+zm8VVGW0FZuO0Cq2PsbnwJAWY78Q6tniNv4edWcyLp35o1p1ZclSIgx5NQBDccJQbSo4Gw+kGrlCMDhkvv4TDsH7nRRNpoDyK2afeUS/uAF7QJBAMoIA3FmRc/ToYmGhAJuvLc9TnTN44EEsQ6QcVKtTTSuFod6R5v6RtnX/knNrJjrToAjxClklQ8HZtkNAcXOSEo=";
	
	 @Override
	 protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) 
	         throws BeansException { 
		 String password = props.getProperty("jdbc.password"); 
		 String userName = props.getProperty("jdbc.username"); 
		 if (password != null) { 
	         try {
	        	 //解密jdbc.password属性值，并重新设置 
	        	 props.setProperty("jdbc.password", new String(RSAUtil.decryptByPrivateKey(BASE64Util.decryptBASE64(password), PRIVATEKEY))); 
	         }catch(Exception e) {
	        	 e.printStackTrace();
	         }
		 } 
		 if (userName != null) { 
			 try {
				 //解密jdbc.username属性值，并重新设置 
				 props.setProperty("jdbc.username", new String(RSAUtil.decryptByPrivateKey(BASE64Util.decryptBASE64(userName), PRIVATEKEY))); 
	         }catch(Exception e) {
	        	 e.printStackTrace();
	         }
		 }
		 super.processProperties(beanFactory, props); 
	 }  
}
